﻿using System.Data;
using System.Web;
using System.Web.SessionState;
using System.IO;
using System.Text;
using System.Data.OleDb;
using LyData.Data;
using System;
using LyData.Data.Xml;


/// <summary>
///CreateHtml 的摘要说明
/// </summary>
public class CreateHtml
{
    public CreateHtml()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    /// <summary>
    /// 生成联系我们
    /// </summary>
    /// <param name="Templet">模板路径</param>
    public static void CreateContact(string Templet)
    {
        Encoding code = Encoding.GetEncoding("gb2312");
        //读取模板文件
        string temp = HttpContext.Current.Server.MapPath(Templet);
        StreamReader sr = null;
        StreamWriter sw = null;
        string str = "";
        try
        {
            sr = new StreamReader(temp, code);
            str = sr.ReadToEnd();
        }
        catch (System.Exception exp)
        {
            HttpContext.Current.Response.Write(exp.Message);
            HttpContext.Current.Response.End();
            sr.Close();
        }
        finally
        {
            sr.Close();
            sr.Dispose();
        }

        string tempstr = str;
        string htmlfilename = HttpContext.Current.Server.MapPath("../Contact.html");//生成的名称

        tempstr = ReplaceKey(tempstr);//网站头、版权信息
        tempstr = tempstr.Replace("$GetFriendLink$", GetFriendLink());//生成友情链接
        tempstr = tempstr.Replace("$GetNavigation$", GetNavigation());//生成导航条

        //写文件
        try
        {
            sw = new StreamWriter(htmlfilename, false, code);
            sw.Write(tempstr);
            sw.Flush();
        }
        catch (System.Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
        }
        finally
        {
            sw.Close();
            sw.Dispose();
        }
    }


    /// <summary>
    /// 生成新闻单页
    /// </summary>
    /// <param name="dt">DataRow  一行记录</param>
    /// <param name="Templet">string  相对路径</param>
    public static void CreateNewsHtml(DataRow dt, string Templet)
    {
        Encoding code = Encoding.GetEncoding("GB2312");
        //读取模板文件
        string temp = HttpContext.Current.Server.MapPath(Templet);
        StreamReader sr = null;
        StreamWriter sw = null;
        string str = "";
        try
        {
            sr = new StreamReader(temp, code);
            str = sr.ReadToEnd();
        }
        catch (Exception exp)
        {
            HttpContext.Current.Response.Write(exp.Message);
            HttpContext.Current.Response.End();
            sr.Close();
        }

        string htmlfilename = HttpContext.Current.Server.MapPath(dt["htmladdress"].ToString());

        str = ReplaceKey(str);//获取网站头信息和版权

        //str = str.Replace("$sLaiy$", dt["Source"].ToString());//laiyuan
        //str = str.Replace("$sName$", dt["athor"].ToString());
        //str = str.Replace("$sTime$", dt["addtime"].ToString());
        //str = str.Replace("$title$", dt["title"].ToString());
        //str = str.Replace("$content$", StringUtil.HtmlDiscode(dt["content"].ToString()));

        str = str.Replace("$GetFriendLink$", GetFriendLink());//友情链接
        str = str.Replace("$GetNavigation$", GetNavigation());//导航条

        //写文件
        try
        {
            sw = new StreamWriter(htmlfilename, false, code);
            sw.Write(str);
            sw.Flush();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
        }
        finally
        {
            sr.Close();
            sr.Dispose();

            sw.Close();
            sw.Dispose();
        }
    }



    /// <summary>
    /// 生成产品单页
    /// </summary>
    /// <param name="dt">DataRow  一行记录</param>
    /// <param name="Templet">string  相对路径</param>
    public static void CreateProductHtml(DataRow dt, string Templet)
    {
        Encoding code = Encoding.GetEncoding("gb2312");
        //读取模板文件
        string temp = HttpContext.Current.Server.MapPath(Templet);
        StreamReader sr = null;
        StreamWriter sw = null;
        string str = "";
        try
        {
            sr = new StreamReader(temp, code);
            str = sr.ReadToEnd();
        }
        catch (Exception exp)
        {
            HttpContext.Current.Response.Write(exp.Message);
            HttpContext.Current.Response.End();
            sr.Close();
        }
        //获取相对地址路径
        string htmlfilename = HttpContext.Current.Server.MapPath(dt["htmladdress"].ToString());

        str = ReplaceKey(str);//获取公司简介

        //str = str.Replace("$PName$", dt["PName"].ToString());
        //str = str.Replace("$PAddress$", dt["PAddress"].ToString());    
        //str = str.Replace("$title$", dt["PName"].ToString());
        //str = str.Replace("$sLaiy$", dt["Source"].ToString());//laiyuan
        //str = str.Replace("$sName$", dt["athor"].ToString());
        //str = str.Replace("$sTime$", dt["addtime"].ToString());
        //str = str.Replace("$Info$", StringUtil.HtmlDiscode(dt["Info"].ToString()));

        str = str.Replace("$GetNavigation$", GetNavigation());//获取导航
        str = str.Replace("$GetFriendLink$", GetFriendLink());//获取友情链接

        //写文件
        try
        {
            sw = new StreamWriter(htmlfilename, false, code);
            sw.Write(str);
            sw.Flush();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
        }
        finally
        {
            sr.Close();
            sr.Dispose();

            sw.Close();
            sw.Dispose();
        }
    }






    /// <summary>
    /// 创建首页
    /// </summary>
    /// <param name="Templet">首页模板的路径</param>
    public static void CreateIndex(string Templet)
    {
        Encoding code = Encoding.GetEncoding("gb2312");//编码方式
        //读取模板文件
        string temp = HttpContext.Current.Server.MapPath(Templet);//得到模板路径
        StreamReader sr = null;
        StreamWriter sw = null;
        string str = "";
        try
        {
            sr = new StreamReader(temp, code);//读取数据流
            str = sr.ReadToEnd();
        }
        catch (Exception exp)
        {
            HttpContext.Current.Response.Write(exp.Message);
            HttpContext.Current.Response.End();
            sr.Close();
        }


        string htmlfilename = HttpContext.Current.Server.MapPath("../Index.html");//创建首页
        string CompanyInfo = "";
        IDb db = new DbFactory().DriverDb();
        db.Open();
        DataRow row = db.TopRow("Title,PageContent", "singlePage", string.Format("MenuId={0} and FileName='{1}'", 3, "introduction"), "");   //获取公司简介
        db.Close();

        str = ReplaceKey(str);//获取网站的头信息和版权

        str = str.Replace("$CompanyInfo$", row["PageContent"].ToString());
        //str = str.Replace("$JSInfo$", GetZhuanjia());
        //str = str.Replace("$JSCode$", GetCode());
        //str = str.Replace("$News$", GetNews());
        //str = str.Replace("$Invests$", GetInvests());
        str = str.Replace("$GetNavigation$", GetNavigation());//获取导航
        str = str.Replace("$GetFriendLink$", GetFriendLink());//获取友情链接


        //写文件
        try
        {
            sw = new StreamWriter(htmlfilename, false, code);
            sw.Write(str);
            sw.Flush();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
        }

        sr.Close();
        sr.Dispose();

        sw.Close();
        sw.Dispose();
    }

    /// <summary>
    /// 创建导航
    /// </summary>
    /// <returns>string</returns>
    public static string GetNavigation()  //导航
    {
        string GetNavigation = "<ul><li><a href='../../index.html'><img src='../../image/home1.gif' /></a></li><li><a href='../../About.html'><img src='../../image/home2.gif' /></a></li><li><a href='../../pet.aspx'><img src='../../image/home3.gif' /></a></li><li><a href='../../Invest.aspx'><img src='../../image/home4.gif' /></a></li><li><a href='../../Expert.aspx'><img src='../../image/home5.gif' /></a></li><li><a href='../../information.html'><img src='../../image/home6.gif' /></a></li></li><li><a href='#'><img src='../../image/home7.gif' /></a></li></li><li><a href='../../Case.aspx'><img src='../../image/home8.gif' /></a></li></li><li><a href='../../online.aspx'><img src='../../image/home9.gif' /></a></li></ul>";

        return GetNavigation;
    }


    public static string GetAD(string type, string id, string istrue)
    {
        string strtemp = "";
        string strwhere = "1=1";
        IDb db = new DbFactory().DriverDb();
        db.Open();
        if (type == "1")
        {
            strwhere = string.Format("{0} and codeClassid={1} and cid={2}", strwhere, type, id);
            DataRow row = db.TopRow("Title,CUrl,imgpath,cwidth,cheight", "Codelist", strwhere, "weight Asc,cid Asc");
            db.Close();
            if (row != null)
            {

                if (istrue == "1")//带连接
                {
                    strtemp = "<a href=\"" + row["CUrl"].ToString() + "\"><img src=\"" + row["imgpath"].ToString() + "\" width=\"" + row["cwidth"].ToString() + "\" height=\"" + row["cheight"].ToString() + "\" alt=\"\" /></a>";
                }
                else
                {
                    strtemp = "<img src=\"" + row["imgpath"].ToString() + "\" width=\"" + row["cwidth"].ToString() + "\" height=\"" + row["cheight"].ToString() + "\" alt=\"\" />";
                }
            }
        }
        else
        {
            strwhere = string.Format("codeClassid={0}", 2);
            DataTable table = db.DataTable("top 4 Title,CUrl,imgpath,cwidth,cheight", "Codelist", strwhere, "weight Asc,cid Asc");
            db.Close();

            if(table!=null)
            {
                for (int i = 0; i < table.Rows.Count;i++ )
                {
                    strtemp += "<td class=\"td_f\"><a href=\"" + table.Rows[i]["CUrl"].ToString() + "\" target=\"_blank\"><img src=\"" + table.Rows[i]["imgpath"].ToString() + "\" width=\"" + table.Rows[i]["cwidth"].ToString() + "\" height=\"" + table.Rows[i]["cheight"].ToString() + "\" alt=\"\" /></a></td>";
                }
            }
        }

        return strtemp;
    }

    /// <summary>
    /// 获取友情链接
    /// </summary>
    /// <returns>string</returns>
    public static string GetFriendLink()
    {
        string GetFriendLink = "";
        string whereStr = string.Format("SiteLanguage={0} ", 0);
        IDb db = new DbFactory().DriverDb();
        db.Open();
        DataTable dtFriendLink = db.DataTable("FriendLinkId,Title,Type,Url,LogoPath", "FriendLink", whereStr, "FriendLinkId DESC");
        db.Close();
        for (int i = 0; i < dtFriendLink.Rows.Count; i++)
        {
            //GetFriendLink += "<li><a href=\"" + dtFriendLink.Rows[i]["Linkaddress"].ToString() + "\" target=\"_blank\" title=\"" + dtFriendLink.Rows[i]["Linkname"].ToString() + "\">" + dtFriendLink.Rows[i]["Linkname"].ToString() + "</a></li>";
        }
        dtFriendLink = null;
        return GetFriendLink;
    }

    /// <summary>
    /// 获取网站的头信息和版权信息
    /// </summary>
    /// <param name="str">模板字符串</param>
    /// <returns>转换后的字符串</returns>
    public static string ReplaceKey(string str)
    {
        DataRow row = new XmlProvider(HttpContext.Current.Server.MapPath("~/App_Data/Website.xml")).ReadNode(string.Format("descendant::Info[Language={0}]", str));
        str = str.Replace("$WebTitle$", row["WebsiteName"].ToString());
        str = str.Replace("$NewsNum$", row["WebsiteTitle"].ToString());
        str = str.Replace("$KeyWord$", row["KeyWords"].ToString());
        str = str.Replace("$Describe$", row["Description"].ToString());
        str = str.Replace("$CopyRight$", row["CopyRight"].ToString());
        return str;
    }


    /// <summary>
    /// 删除服务器上的文件
    /// </summary>
    /// <param name="htmladdress">文件相对于服务器上的目录</param>
    public static void DeleteFile(string htmladdress)
    {
        string path = HttpContext.Current.Server.MapPath(htmladdress);
        if (File.Exists(path))
        {
            File.Delete(path);
        }
    }
}
